package com.lingxin.gps.sdk;

import com.alibaba.fastjson.JSONObject;
import com.pisces.tool.query.QueryObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author pisces
 * @date 2017/12/18
 */
public class GpsDeviceApi {

    private static Logger logger = LoggerFactory.getLogger(GpsDeviceApi.class);

    public static JSONObject userLogin(String userName, String password) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        StringBuilder sb = new StringBuilder("/Service/user/login");
        sdk.setApiPath("/Service/user/login");
        sdk.addParams("userName", userName);
        sdk.addParams("password", password);
        return GpsDeviceUtil.data(sdk.invoke());
    }

    public static void userLogout(long uid, String token) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/user/logout");
        sdk.addParams("uid", uid + "");
        sdk.addParams("token", token);
        sdk.invoke();
    }

    public static JSONObject userInfo(long uid, String token) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/user/info");
        sdk.addParams("uid", uid + "");
        sdk.addParams("token", token);
        return GpsDeviceUtil.data(sdk.invoke());
    }

    public static boolean userSaveInfo(Long uid, String token, String nickName, String mobile) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/user/saveInfo");
        sdk.addParams("uid", uid + "");
        sdk.addParams("token", token);
        sdk.addParams("nickName", nickName);
        sdk.addParams("mobile", mobile);
        return GpsDeviceUtil.isSucc(sdk.invoke());
    }

    public static boolean userModifyPwd(Long uid, String token, String oldPassword, String newPassword, String confirmPassword) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/user/modifyPwd");
        sdk.addParams("uid", uid + "");
        sdk.addParams("token", token);
        sdk.addParams("oldPassword", oldPassword);
        sdk.addParams("newPassword", newPassword);
        sdk.addParams("confirmPassword", confirmPassword);
        return GpsDeviceUtil.isSucc(sdk.invoke());
    }

    public static JSONObject deviceList(long uid, String token, QueryObject queryObject) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/device/deviceList");
        sdk.addParams("uid", uid + "");
        sdk.addParams("token", token);
        if(queryObject.getQuery("deviceCode") != null){
            sdk.addParams("deviceCode", queryObject.getQuery("deviceCode"));
        }
        return GpsDeviceUtil.data(sdk.invoke());
    }

    public static JSONObject deviceDetail(long uid, String token, String deviceCode) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/device/detail");
        sdk.addParams("uid", uid + "");
        sdk.addParams("token", token);
        sdk.addParams("deviceCode", deviceCode);
        return GpsDeviceUtil.data(sdk.invoke());
    }

    public static JSONObject deviceWeldList(long uid, String token, String deviceCode, QueryObject queryObject) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/device/weldList");
        sdk.addParams("uid", uid + "");
        sdk.addParams("token", token);
        sdk.addParams("deviceCode", deviceCode);
        if(queryObject.getQuery("startTime") != null){
            sdk.addParams("startTime", queryObject.getQuery("startTime"));
        }
        if(queryObject.getQuery("endTime") != null){
            sdk.addParams("endTime", queryObject.getQuery("endTime"));
        }
        if(queryObject.getQuery("weldResult") != null){
            sdk.addParams("weldResult", queryObject.getQuery("weldResult"));
        }
        if(queryObject.getQuery("size") != null){
            sdk.addParams("size", queryObject.getQuery("size"));
        }
        if(queryObject.getQuery("orderByWeldTime") != null){
            sdk.addParams("orderByWeldTime", queryObject.getQuery("orderByWeldTime"));
        }
        return GpsDeviceUtil.data(sdk.invoke());
    }

    public static JSONObject devicePosition(long uid, String token, String deviceCode) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/device/position");
        sdk.addParams("uid", uid);
        sdk.addParams("token", token);
        sdk.addParams("deviceCode", deviceCode);
        return GpsDeviceUtil.data(sdk.invoke());
    }

    public static JSONObject deviceTrack(long uid, String token, String deviceCode, QueryObject queryObject) {
        GpsDeviceClient sdk = new GpsDeviceClient();
        sdk.setApiPath("/Service/device/track");
        sdk.addParams("uid", uid);
        sdk.addParams("token", token);
        sdk.addParams("deviceCode", deviceCode);
        sdk.addParams("startTime", queryObject.getQuery("startTime"));
        sdk.addParams("endTime", queryObject.getQuery("endTime"));
        if(queryObject.getQuery("speed") != null){
            sdk.addParams("speed", queryObject.getQuery("speed"));
        }
        return GpsDeviceUtil.data(sdk.invoke());
    }
}
